package com.sto.stoapp.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.sto.stoapp.StoApplication;

import sto.android.app.StoPdaKeyEvent;

@SuppressLint("NewApi")
public class StoHhmEditText extends StoEditText {

	private EditClickListener editDelete;

	// private boolean isPadCenter = false;
	private Context mContext;
	private boolean isBackKey = false;

	private EditScanListener scanListener;

	public StoHhmEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		init();
	}

	public StoHhmEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		init();
	}

	public StoHhmEditText(Context context) {
		super(context);
		mContext = context;
		init();
	}

	InputMethodManager imm;

	private void init() {
		setSingleLine();
		setRawInputType(InputType.TYPE_CLASS_PHONE);
		//closeBoard();
		//编辑框监听焦点，监听扫描动作
		this.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (!hasFocus) {

					if (scanListener != null) {
						scanListener.offScan();
					}
				} else {
					isBackKey = true;
					if (scanListener != null) {
						scanListener.onScan();
					}
				}
			}
		});

		this.setOnKeyListener(new OnKeyListener() {

			/**
			 * 修改小退功能键
			 * 
			 * @修改 mgf
			 */
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (event.getAction() == KeyEvent.ACTION_UP) {
					keyCode= StoApplication.getStoJni().getEventFuction(keyCode);
					if (keyCode == StoPdaKeyEvent.KEYCODE_DPAD_CENTER && hasFocus()) {
						// if (!isPadCenter) {
						// isPadCenter = true;
						// } else {
						if (editDelete != null) {
							editDelete.onEditClickListener(v);
							StoHhmEditText.this.cancelLongPress();
							return true;
						}

						// }
					}
				}
				return false;

			}
		});
		setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (editDelete != null)
					editDelete.onEditClickListener(v);
			}
		});
	}

	/**
	 * 屏蔽虚拟键盘的小退功能
	 * 
	 * @author mgf
	 * @return 返回true时，不处理onkey事件
	 */
	@Override
	public boolean onKeyPreIme(int keyCode, KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN
				&& StoHhmEditText.this.getText().toString().length() > 0
				&& hasFocus()) {
			isBackKey = false;
			return false;
		}
		if (event.getAction() == KeyEvent.ACTION_UP
				&& keyCode == StoPdaKeyEvent.KEYCODE_DEL
				&& StoHhmEditText.this.getText().toString().length() <= 0
				&& hasFocus()) {
			if (isBackKey && editDelete != null) {
				// isBackKey = false;
				editDelete.onEditDelete();
				return true;
			} else {
				isBackKey = true;
				return false;
			}
			// editDelete.onEditDelete();
			// return true;
		}
		return false;
	}

	public void setEditDelete(EditClickListener editDelete) {
		this.editDelete = editDelete;
	}

	public interface EditClickListener {

		void onEditDelete();

		void onEditClickListener(View v);

	}

	public void setEditScanListener(EditScanListener editListener) {
		this.scanListener = editListener;
	}

	public interface EditScanListener {

		void onScan();

		void offScan();
	}

	/**
	 * 调用输入法
	 */
	public void showSRF() {
		InputMethodManager inputManager =

		(InputMethodManager) this.getContext().getSystemService(
				Context.INPUT_METHOD_SERVICE);

		inputManager.showSoftInput(this, 0);
	}
}
